<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author tommy.pan
 */

namespace app\common\model\jooj_member;

class VipsAddress extends Base {

    protected $base_table = 'jj_vips_address';
    protected $table = 'jj_vips_address';
    protected $connection = ['database' => 'jooj_member'];
    protected $_isDefCfg = [0 => '非默认', 1 => '默认地址'];

    // protected $resultSetType = 'collection';

    public function __construct($companyId = []) {
        if (!is_array($companyId)) {
            $this->table = $this->base_table . '_' . $companyId;
        }
        parent::__construct($companyId);
    }

    /**
     * 获取会员列表
     * @param type $where
     * @param type $page
     * @param type $pageSize
     * @return boolean
     */
    public function getVipAddressList($where, $page = 1, $pageSize = 10) {
        $pageList = $this->getPageCount($where, $page, $pageSize);
        return $pageList;
    }

    /**
     * 添加地址
     * @param type $data
     * @return type
     */
    public function addMemberAddress($data) {
        $data = $this->toUnderScore($data);
        if (1 == $data['is_def']) {
            $update = $this->where(['member_code' => $data['member_code']])->data(['is_def' => 0])->update();
        }
        $add = $this->insertGetId($data);
        return $add;
    }

    /**
     * 更新地址
     * @param type $where
     * @param type $data
     * @return type
     */
    public function updateMemberAddress($where, $data) {
        $data = $this->toUnderScore($data);
        if (1 == $data['is_def']) {
            $update = $this->where(['member_code' => $data['member_code']])->data(['is_def' => 0])->update();
        }
        $update = $this->where($where)->data($data)->update();
        return $update;
    }

    /**
     * 列表数据分析（延续之前逻辑）
     */
    function getAddressInfo($list, $extends = array()) {
        if (!is_array($list) || count($list) < 1)
            return false;
        foreach ($list as $k => $v) {
            $list[$k]['addressInfo'] = $this->where(['member_code' => $v['member_code']])->find();
            // $list[$k]['is_def_t'] = $this->_isDefCfg[$v['is_def']];
        }
        return $list;
    }

    /**
     * 删除一个地址
     * @param type $id
     * @return boolean
     */
    public function delOneById($id) {
        if (empty($id)) {
            return false;
        }
        $del = $this->where(['id' => $id])->delete();
        return $del;
    }

    /**
     * 获取单条记录
     * @param type $condition
     * @param type $field
     * @return type
     */
    public function getAddressInfoByConditon($condition, $field = '*') {
        $info = $this->field($field)->where($condition)->find();
        return $info;
    }

    /**
     * 获取多条记录
     * @param type $condition
     * @param type $field
     * @return type
     */
    public function getAddressByConditon($condition, $field = '*') {
        $address = $this->field($field)->where($condition)->select();
        return $address;
    }

}
